package com.vonchange.utils.tpl.method;

import java.io.IOException;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.vonchange.utils.json.JsonUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component
public class ToJsonDirective implements TemplateDirectiveModel {
	private static final String PARAM_BEAN = "bean";
	@SuppressWarnings({ "rawtypes"})
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		Object beanObject=params.get(PARAM_BEAN);
		String result=JsonUtil.toJson(beanObject);
		env.getOut().append(result);
	}

}